package com.leetcode.binary_search;

import java.util.Random;

/**
 * @author Dennis Li
 * @date 2020/10/31 17:05
 */
public class GuessNumber {

    private Random random = new Random();

    private int pick;

    private int guess(int num) {
        return num > pick ? 1 : num < pick ? -1 : num;
    }

    public int guessNumber(int n) {
        int i = 1, j = n;
        while (i <= j) {
            int mid = i + (j - i) / 2;
            int cmp = guess(mid);
            if (cmp == 0)
                return mid;
            else if (cmp > 0)
                j = mid - 1;
            else
                i = mid + 1;
        }
        return -1;
    }

}
